package com.cdplife.ci.modular.system.service.impl;

import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cdplife.ci.core.common.exception.BizExceptionEnum;
import com.cdplife.ci.core.shiro.ShiroKit;
import com.cdplife.ci.modular.system.model.InsuranceKind;
import com.cdplife.ci.modular.system.dao.InsuranceKindMapper;
import com.cdplife.ci.modular.system.service.IInsuranceKindService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.cdplife.ci.modular.system.service.impl.PolicyPlanServiceImpl.SUCCESS_TIP;

/**
 * <p>
 * 险种表 服务实现类
 * </p>
 *
 * @author jers
 * @since 2018-11-07
 */
@Service
public class InsuranceKindServiceImpl extends ServiceImpl<InsuranceKindMapper, InsuranceKind> implements IInsuranceKindService {
    /**
     * 获取保单列表
     */
    @Override
    public List<Map<String, Object>> selectInsuranceKindList(Integer policyId){

        return this.baseMapper.selectInsuranceKindList(policyId);
    }
    /**
     * 增加险种
     */
    @Override
    public Object addInsuranceKind(InsuranceKind insuranceKind){
        Object result = validate(insuranceKind);
        if(result != null){
            return result;
        }
        if(insuranceKind.getPolicyId() == null){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        try {
            //校验重复保单ID与险种CODE
            Wrapper<InsuranceKind> wrapper = new EntityWrapper<InsuranceKind>();
            wrapper = wrapper.eq("policy_id", insuranceKind.getPolicyId()).and().eq("insurance_kind_code",insuranceKind.getInsuranceKindCode()).and().eq("is_del",0);
            List<InsuranceKind> insuranceKinds = this.baseMapper.selectList(wrapper);
            if (insuranceKinds != null && insuranceKinds.size() > 0) {
                throw new ServiceException(BizExceptionEnum.EXISTED_THE_ERROR);
            }
            //设置当前险种
            insuranceKind.setCreatedBy(ShiroKit.getUser().getId().toString());
            insuranceKind.insert();
            return SUCCESS_TIP;
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    }
    /**
     * 修改险种
     */
    @Override
    public Object updateInsuranceKind(InsuranceKind insuranceKind){
        Object result = validate(insuranceKind);
        if(result != null){
            return result;
        }
        if(insuranceKind.getId() == null){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        try {
            //修改的条件限制
            //to do
            //设置当前险种
            insuranceKind.setLastModifiedBy(ShiroKit.getUser().getId().toString());
            insuranceKind.updateById();
            return SUCCESS_TIP;
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    }
    /**
     * 删除险种
     */
    @Override
    public Object deleteInsuranceKind(Integer insuranceKindId){
        try {
            InsuranceKind insuranceKind = this.baseMapper.selectById(insuranceKindId);
            insuranceKind.setLastModifiedBy(ShiroKit.getUser().getId().toString());
            insuranceKind.setIsDel(1);
            //删除的条件 。。。
            //to do
            insuranceKind.updateById();
            return SUCCESS_TIP;
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    }

    /**
     * @brief 过滤条件
     * @param insuranceKind
     * @return
     */
    private Object validate(InsuranceKind insuranceKind){
        //校验空字符
        if (ToolUtil.isOneEmpty(insuranceKind.getInsuranceKindCode(),
                insuranceKind.getInsuranceKindName(),insuranceKind.getAmountLimit(),insuranceKind.getAmount(),
                insuranceKind.getAmountType())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //险种code大于4位
        if(insuranceKind.getInsuranceKindCode().length() > 4){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //校验保费比例
        if(insuranceKind.getAmountType() == 2 && insuranceKind.getAmount().compareTo(new BigDecimal("100")) > 0){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        return null;
    }
}
